package com.wzx.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzx.common.exception.LeadNewsException;
import com.wzx.user.dto.RelationDto;
import com.wzx.user.mapper.GroupMapper;
import com.wzx.user.mapper.RelationMapper;
import com.wzx.user.mapper.UserMapper;
import com.wzx.user.mongo.SystemMsg;
import com.wzx.user.pojo.Group;
import com.wzx.user.pojo.Relation;
import com.wzx.user.pojo.User;
import com.wzx.user.service.RelationService;
import com.wzx.user.vo.GroupChangeRemarkVo;
import com.wzx.user.vo.RelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/7/31 - 15:40
 * @description 标题
 */
@Service
public class RelationServiceImpl implements RelationService {

    @Autowired
    private RelationMapper relationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<RelationDto> getRelationList(Integer userId, Integer relationType) {
        if (relationType == 4) {
            return relationMapper.getRelationListByFs(userId);
        } else if (relationType == 3) {
            return relationMapper.getRelationListByGroup(userId);
        } else if (relationType == 5) {
            return relationMapper.getRelationListByBlack(userId);
        } else {
            return relationMapper.getRelationList(userId, relationType);
        }
    }

    @Override
    public Relation queryRelationState(Integer userId, Integer friendId) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId);
        queryWrapper.eq("fid", friendId);
        Relation relation = relationMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(relation.getNotWatchState())) {
            relation.setNotWatchState("0");
        }
        if (StringUtils.isEmpty(relation.getBlacklist())) {
            relation.setBlacklist("0");
        }

        return relation;
    }

    @Override
    public void changeState(Relation relation) {
        relationMapper.changeState(relation);
    }

    @Override
    public String queryFriendRemarks(Relation relation) throws LeadNewsException {
        //1，查询关系表看是否为朋友设置了备注
        QueryWrapper<Relation> relationQueryWrapper = new QueryWrapper<>();
        relationQueryWrapper.eq("uid", relation.getUid());
        relationQueryWrapper.eq("fid", relation.getFid());
        relationQueryWrapper.eq("type", relation.getType());
        Relation resultRelation = relationMapper.selectOne(relationQueryWrapper);
        if (resultRelation == null) {
            throw new LeadNewsException("未建立关系");
        }

        if (!StringUtils.isEmpty(resultRelation.getFriendRemarks())) {
            return resultRelation.getFriendRemarks();
        }
        if (relation.getType().equals("2")) {
            User user = userMapper.selectById(relation.getFid());
            return user.getNickname();
        } else {
            Group group = groupMapper.selectById(relation.getFid());
            return group.getGroupName();
        }

    }

    @Override
    public String updateFriendRemarks(Relation relation) {
        //if (relation.getType())
        relationMapper.updateFriendRemarks(relation);
        if (StringUtils.isEmpty(relation.getFriendRemarks())) {
            User user = userMapper.selectById(relation.getFid());
            return user.getNickname();
        }
        return relation.getFriendRemarks();
    }

    @Override
    @Transactional
    public void deleteFriendRemarks(Relation relation) throws LeadNewsException {
        //查询是否存在好友关系
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", relation.getUid());
        queryWrapper.eq("fid", relation.getFid());
        queryWrapper.eq("type", "2");
        Relation relationObject = relationMapper.selectOne(queryWrapper);

        if (relationObject == null) {
            throw new LeadNewsException("还未建立好友关系，请先添加好友！");
        }

        relationMapper.deleteById(relationObject.getRelationId());
        QueryWrapper<Relation> deleteQueryWrapper = new QueryWrapper<>();
        deleteQueryWrapper.eq("fid", relation.getUid());
        deleteQueryWrapper.eq("uid", relation.getFid());
        queryWrapper.eq("type", "2");
        relationMapper.delete(deleteQueryWrapper);

        //删除系统消息中添加好友数据
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.orOperator(
                Criteria.where("senderId").is(relation.getUid()).and("receiverId").is(relation.getFid()),
                Criteria.where("senderId").is(relation.getFid()).and("receiverId").is(relation.getUid()));
        query.addCriteria(criteria);

        mongoTemplate.remove(query, SystemMsg.class);

    }

    @Override
    public void changeRemarksByGroup(GroupChangeRemarkVo groupChangeRemarkVo) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", groupChangeRemarkVo.getUserId());
        queryWrapper.eq("fid", groupChangeRemarkVo.getFriendId());
        queryWrapper.eq("type", "3");
        Relation rel = relationMapper.selectOne(queryWrapper);
        rel.setIInGroupRemarks(groupChangeRemarkVo.getGroupRemark());
        relationMapper.updateById(rel);
    }

    @Override
    public Boolean queryIsRelation(Integer userId, Integer friendId) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId);
        queryWrapper.eq("fid", friendId);
        Relation rel = relationMapper.selectOne(queryWrapper);
        return rel == null;
    }

    @Override
    public void addBlackUser(Integer userId, Integer friendId, String changeType) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId);
        queryWrapper.eq("fid", friendId);
        Relation rel = relationMapper.selectOne(queryWrapper);

        rel.setBlacklist(changeType);
        relationMapper.updateById(rel);

    }

    @Override
    public String queryIsBlack(Integer userId, Integer friendId) throws LeadNewsException {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", friendId);
        queryWrapper.eq("fid", userId);
        Relation rel = relationMapper.selectOne(queryWrapper);
        if (rel == null) {
            throw new LeadNewsException("还未建立好友关系！");
        }

        if (StringUtils.isEmpty(rel.getBlacklist())) {
            return "1";
        }
        return rel.getBlacklist();
    }

    @Override
    public List<Relation> queryAllUserFriendRemarks(Integer userId) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId);
        queryWrapper.eq("type", "2");
        queryWrapper.isNotNull("friendRemarks");
        return relationMapper.selectList(queryWrapper);
    }

    @Override
    public List<RelationDto> getRelationListBySearch(RelationVo relationVo) {
        return relationMapper.getRelationListBySearch(relationVo);
    }
}
